package com.framework.macros;

import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Space;

/**
 * Este es un componente que me ayuda a tomar la hora 
 * para el usuario, ya que el componente de hora no es sencillo.
 * @author Luis Miguel
 * */
public class HoraTextBox extends Hbox {
	
	public static Logger log = Logger.getLogger(HoraTextBox.class);
	
	private Label labelHORA;
	private Space spaceHORA;
	
	private Listbox  listboxHH;
	private Listbox  listboxMM;
	private Listbox  listboxMERIDIANO;
	
	
	private OnCambioHora onCambioHora;
	
	public HoraTextBox() {
		cargarComponents();
	    inicializar();
	}
  
	private void cargarComponents() {
		labelHORA = new Label();
		listboxHH = new Listbox();
		listboxMM = new Listbox();
		spaceHORA = new Space();
		spaceHORA.setVisible(false);
		listboxMERIDIANO = new Listbox();
		
		listboxHH.setMold("select");
		listboxMM.setMold("select");
		listboxMERIDIANO.setMold("select");
		
		listboxHH.addEventListener(Events.ON_SELECT, getEventListener());
		listboxMM.addEventListener(Events.ON_SELECT, getEventListener());
		listboxMERIDIANO.addEventListener(Events.ON_SELECT, getEventListener());
		
		// Inyectamos los componentes
		appendChild(labelHORA);
		appendChild(spaceHORA);
		appendChild(listboxHH);
		appendChild(listboxMM);
		appendChild(listboxMERIDIANO);
	}
	
	
	private EventListener<Event> getEventListener(){
		return new EventListener<Event>() {
			@Override
			public void onEvent(Event arg0) throws Exception {
				if(onCambioHora != null){
					onCambioHora.onCambio(HoraTextBox.this); 
				}
			}
		};
	}

	private void inicializar() {
        inicicializarSecuencia();   		
	}
	
	/**
	 * Este metodo me permite darle un ancho
	 * a los componentes hijos
	 * @author Luis Miguel
	 * */
	public void setWidth_(int width){
		setWidth(width + "px");  
		int w_listbox_hora = 35 * width / 100;
		int w_listbox_minuto = 35 * width / 100;
		int w_listbox_meridiano = 30 * width / 100;
		
		listboxHH.setWidth(w_listbox_hora + "px");
		listboxMM.setWidth(w_listbox_minuto + "px");
		listboxMERIDIANO.setWidth(w_listbox_meridiano + "px");
	}
	
	public void setLabel_(String label){
		if(label != null && label.isEmpty()){
			labelHORA.setValue(label);
			spaceHORA.setVisible(true);
		}else{
			spaceHORA.setVisible(false);
		}
	}
	
	
	private void inicicializarSecuencia(){
		listboxMERIDIANO.appendChild(new Listitem("AM", "AM"));
		listboxMERIDIANO.appendChild(new Listitem("PM", "PM"));
		for (int i = 0; i < 60; i++) {
			 String ii = (i <= 9 ? ("0" + i) : (i + "")); 
			 if(i > 0 && i < 13){
				 listboxHH.appendChild(new Listitem(ii, ii));
			 }
			 listboxMM.appendChild(new Listitem(ii, ii));
		}
		seleccionar(listboxHH);
		seleccionar(listboxMM);
		seleccionar(listboxMERIDIANO);
	}

	private void seleccionar(Listbox listbox) { 
       if(listbox.getItemCount() > 0){
    	   listbox.setSelectedIndex(0); 
       }
	}
	
	public void setValue(Date date) { 
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int hh = calendar.get(Calendar.HOUR);
		int mm = calendar.get(Calendar.MINUTE);
		int am_pm = calendar.get(Calendar.AM_PM);
		
		listboxHH.setSelectedIndex(hh);
		listboxMM.setSelectedIndex(mm);
		listboxMERIDIANO.setSelectedIndex(am_pm);
//		//log.info("hh: " + hh + ": mm: " + mm + " AM_PM: " + am_pm); 
	}

	public Date getValue() { 
		Date date = getTimeString();
//		//log.info("Hora: " + new SimpleDateFormat("hh:mm a").format(date)); 
		return date; 
	}
	  
	private Date getTimeString(){
		Calendar  calendar = Calendar.getInstance();
		
//		//log.info("AM / PM: " + calendar.get(Calendar.AM_PM));  
		int hora = listboxHH.getSelectedIndex() + 1;
		 
//		//log.info("Hora: " + hora);
		calendar.set(Calendar.MINUTE, listboxMM.getSelectedIndex());
		if(listboxMERIDIANO.getSelectedItem().getValue().equals("PM") && hora != 12){
			hora += 12;
		}else if(hora == 12 && listboxMERIDIANO.getSelectedItem().getValue().equals("AM")){
			hora = 00;
		}
//		//log.info("Hora final: " + hora);
		
		calendar.set(Calendar.HOUR_OF_DAY, hora);
		calendar.set(Calendar.MILLISECOND, 0); 
		
//		//log.info("Hora: " + calendar.getTime()); 
		return calendar.getTime();
	}
	
	public interface OnCambioHora{
		void onCambio(HoraTextBox horaTextBox);
	}

	public OnCambioHora getOnCambioHora() {
		return onCambioHora;
	}

	public void setOnCambioHora(OnCambioHora onCambioHora) {
		this.onCambioHora = onCambioHora;
	}
}
